package cineplanet.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import javax.sql.DataSource;

import cineplanet.excepcion.DAOExcepcion;
import cineplanet.modelo.Horario;
public class HorarioDAOImpl extends BaseDAO implements HorarioDAO {
	private DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}
	
	@Override
	public Collection<Horario> listar() throws DAOExcepcion {
		Collection<Horario> c = new ArrayList<Horario>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = dataSource.getConnection();
			String query = "SELECT idHorario,fechaProyeccion,hora from cineplanet.horario h";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Horario vo = new Horario();
				vo.setIdHorario(rs.getInt("idCine"));
				vo.setFechaProyeccion(rs.getDate("fechaProyeccion"));
				vo.setHora(rs.getString("hora"));
				c.add(vo);
			}

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}
}
